package org.jaaksi.customviewdemo.heart

import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_ecg.*
import org.jaaksi.customviewdemo.R
import java.util.*
import kotlin.collections.ArrayList

class EcgActivity : AppCompatActivity() {

    var list = ArrayList<Int>()
    val timer = Timer()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ecg)
        init()
    }

    fun init() {

        timer.schedule(object : TimerTask() {
            override fun run() {
                handler.sendEmptyMessage(0)

            }
        }, 100, 200)
    }


    var i = 1
    var handler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            var r = 1 + Random().nextInt(10)
            i++
            if (i == 10) {
                r -= 200
            }
            if (i == 20) {
                r = 55
            }
            if (i == 21) {
                r = 45
            }
            if (i == 22) {
                r = 50
            }
            if (i == 30) {
                r = 0//心形位置
            }
            if (i == 40) {
                i = 0
            }
            println(r)
            ecg_view.setData(r)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }
}
